package com.jbinfo.qingcaowan.base.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jbinfo.common.utils.UUIDUtils;
import com.jbinfo.qingcaowan.base.dao.BaseSignDao;
import com.jbinfo.qingcaowan.base.dto.BaseSignDTO;
import com.jbinfo.qingcaowan.base.entity.BaseSign;
import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;
import com.jbinfo.qingcaowan.security.entity.User;
import com.jbinfo.qingcaowan.user.dao.ScoreDetailDAO;
import com.jbinfo.qingcaowan.user.dao.ScoreSetDAO;
import com.jbinfo.qingcaowan.user.dao.UserDao;
import com.jbinfo.qingcaowan.user.domain.ScoreDetailDomain;

/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO 用户管理service实现类
* @author ZhangLei 
* @date 2016年6月22日 上午10:23:02
 */
@Service("baseSignService")
public class BaseSignServiceImpl implements BaseSignService {

	@Resource(name="baseSignDao")
	private BaseSignDao baseSignDao;
	
	@Resource(name="scoreSetDAO")
	private ScoreSetDAO scoreSetDAO;
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="scoreDetailDAO")
	private ScoreDetailDAO scoreDetailDAO;
	
	// 签到
	@Override
	public Object saveSign(BaseSignDTO queryDto) {
		
		if (queryDto.getUserId() == null || queryDto.getUserId().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空!");
		}
		// 根据userId 获取orgId
		String orgId = "";
		String userId = queryDto.getUserId();
		User user = (User) userDao.selectByPrimaryKey(userId);
		orgId = user.getUserOrg();
		
		BaseSign baseSign = ClonePojoUtil.copyClassFromTo(queryDto, BaseSign.class);
		
		// 查询当天签到状态
		List<BaseSign> baseSignList = baseSignDao.selectSignStatus(baseSign);
		if (baseSignList != null && !baseSignList.isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.database_duplicate_data, "今天您已签到");
		}
		
		baseSign.setId(UUIDUtils.create());
		
		int nRet = baseSignDao.insertSelective(baseSign);
		
		String strSignMessage = "";
		
		if (nRet > 0) {
			strSignMessage = "signSuccess";
			
			// 插入学分详情表
			String type = "0002";
			Integer score = scoreSetDAO.findScoreByType(type);
			
			ScoreDetailDomain entity = new ScoreDetailDomain();
			entity.setId(UUIDUtils.create());
			entity.setType(type);
			entity.setOrgId(orgId);
			entity.setUserId(userId);
			entity.setScore(score);
			entity.setCreatUser(userId);
			entity.setCreatTime(new Date());
			entity.setModifyUser(userId);
			entity.setModifyTime(new Date());
			
			scoreDetailDAO.save(entity);
		}
		queryDto.setSignMessage(strSignMessage);
		//将签到时间转换格式为 xxxx-xx-xx xx:xx:xx am
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa",Locale.ENGLISH);
		//变换时间格式
		//SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		queryDto.setStrSignDate(sdf.format(queryDto.getSignDate()));
		return queryDto;
	}

	// 根据signDate、userId查询当天签到状态
	@Override
	public Object getSignStatus(BaseSignDTO baseSignDTO) {

		if (baseSignDTO.getUserId() == null || baseSignDTO.getUserId().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空!");
		}
		
		BaseSign baseSignDomain = ClonePojoUtil.copyClassFromTo(baseSignDTO, BaseSign.class);
		
		List<BaseSign> baseSignList = baseSignDao.selectSignStatus(baseSignDomain);
		
		BaseSignDTO baseSign = new BaseSignDTO();
		if (baseSignList == null || baseSignList.isEmpty()) {
			baseSign.setSignStatus("0"); // 未签到
		} else {
			baseSign.setSignStatus("1"); // 已签到
		}
		
		if (baseSignList.size() > 0) {
			BaseSign sign = baseSignList.get(0);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String strSignDate = sdf.format(sign.getSignDate());
			baseSign.setStrSignDate(strSignDate);
		}
		return baseSign;
	}
}
